 <cfcomponent displayname="Application" hint="The Application.cfc" output="yes">

	<cfset this.name = "Kalendar-0.89" />
	<cfset this.sessionManagement = true />
	<cfset this.loginStorage="session">
	<cfset this.sessionTimeout = createTimeSpan(0,0,60,0) />

	<cffunction name="onApplicationStart">
		<cfscript>
			application.cfcPath = "kalendar.common.cfc";
			
			application.ToolTipUtil = createObject('component','#application.cfcPath#.utils.ToolTipUtil');
			
			// Load LightWire
			application.LightWire = createobject("component", "#application.cfcPath#.LightWire").init( createobject("component", "#application.cfcPath#.config.LightWireConfig").init("common\cfc\config\settings.ini.cfm"));
			application.KalendarConfig = application.LightWire.getTransient("KalendarConfig");
			application.KalendarService = application.LightWire.getTransient("KalendarService");
			// TODO: make this non CF8 specific
			application.javaPaths = [ExpandPath("#application.KalendarConfig.getKalendarURLPath()#java/icu4j.jar"),ExpandPath("#application.KalendarConfig.getKalendarURLPath()#java/kalendar.jar")];
			application.javaLoader = CreateObject("component","#application.cfcPath#.javaloader.JavaLoader").init(application.javaPaths);
			
			application.timezones = CreateObject('component','#application.cfcPath#.Timezones').init(application.javaLoader, application.KalendarConfig.getDSN());
			
			application.timezones.get('America/New_York');
			
			application.UserPreferences = Createobject('component','#application.cfcPath#.UserPreferences').init(application.KalendarConfig.getDSN());
			
		</cfscript>
	</cffunction>

	<cffunction name="onSessionStart">
		<cfscript>
			session.user = CreateObject('component','#application.cfcPath#.User').init();
		</cfscript>	
	</cffunction>
	
	<cffunction name="onRequestStart">
		<cfif structKeyExists(url,'reload') AND url.reload>
			<cfset StructClear(application)/>
			<cfset onApplicationStart()/>
			<cfset session.user = CreateObject('component','#application.cfcPath#.User').init()/>
		</cfif>
		<cfif structKeyExists(url, 'logout')>
			<cfset session.user = CreateObject('component','#application.cfcPath#.User').init()/>
		</cfif>
	</cffunction>
	
	<cffunction name="onRequest">
		<cfargument name="thePage" type="string" required="true">
		<cfset var userInfo = ""/>
		<cfset var i = ""/>
		<cfset var urlVars = ""/>
		<cfif (StructKeyExists(url,'param1') AND StructKeyExists(url,'param2') AND NOT session.user.getIsLoggedIn())>
			<cfset urlVars = ListToArray(cgi.query_String,'&')/>
			<cfloop from="1" to="#ArrayLen(urlVars)#" index="i">
				<cfset url[ListFirst(urlVars[i],'=')] = ListRest(urlVars[i],'=')/>
			</cfloop>
			<cfhttp method="get" url="http://210.18.64.131/MySourceService/MySourceService.asmx/getKalendarUserInfo" result="userInfo">
				<cfhttpparam name="param1" value="#url.param1#" type="url" encoded="true"/>
				<cfhttpparam name="param2" value="#url.param2#" type="url" encoded="true"/>
			</cfhttp>
			<cfif findNoCase('userid',userInfo.fileContent)>
				<cfset session.user.initWithXML(xmlParse(userInfo.FileContent))/>
				<cfset application.UserPreferences.getPreferences(session.user)/>
				<cfset url.view = session.user.getDefaultView()/>
				<cfset session.user.setIsLoggedIn(true)/>
			</cfif>
		</cfif>
		<cfscript>
			request.timezones = application.timezones;
			variables.KalendarRequest = application.LightWire.getTransient("KalendarRequest").init(argumentcollection = application.KalendarService.loadInput(URL, Form));
			variables.KalendarConfig = application.KalendarConfig;
			variables.KalendarService = application.KalendarService;
			variables.LightWire = application.LightWire;
			variables.userPreferences = application.userPreferences;
		</cfscript>
		<cfinclude template="#arguments.thePage#"/>
	</cffunction>
	
	<cffunction name="onRequestEnd">
		<cfif StructKeyExists(request, "relocate")>
			<script>window.location = '<cfoutput>#JSFormat(request.relocate)#</cfoutput>';</script>
		</cfif>
		<cfif isDefined("returnMessage")>
			<!--- TODO: Get rid of this annoying crap. Replace with a jQuery modal --->
			<script>alert("<cfoutput>#JSFormat(returnMessage)#</cfoutput>")</script>	
		</cfif>
	</cffunction>

</cfcomponent>